package city.spring.modules.cardinfo.controller;

import city.spring.modules.cardinfo.entity.CardRechargeEntity;
import city.spring.modules.cardinfo.service.CardRechargeService;
import city.spring.modules.personnel.entity.EmployeeEntity;
import city.spring.modules.personnel.service.EmployeeService;
import city.spring.utils.MyBatisUtils;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * Controller：充值记录
 *
 * @author pxm
 * @date 2020-05-13 10:31:24
 */
@RestController
@RequestMapping("/card_recharge")
public class CardRechargeController extends ApiController {
    private final CardRechargeService cardRechargeService;
    private final EmployeeService employeeService;
    /**
     * 实体类可排序字段
     */
    private final List<SFunction<CardRechargeEntity, ?>> entityOrderFields;

    public CardRechargeController(CardRechargeService cardRechargeService, EmployeeService employeeService) {
        this.cardRechargeService = cardRechargeService;
        this.employeeService = employeeService;
        entityOrderFields = new ArrayList<>();
        entityOrderFields.add(CardRechargeEntity::getId);
        entityOrderFields.add(CardRechargeEntity::getCardMember);
        entityOrderFields.add(CardRechargeEntity::getPerson);
        entityOrderFields.add(CardRechargeEntity::getPersonType);
        entityOrderFields.add(CardRechargeEntity::getBalance);
        entityOrderFields.add(CardRechargeEntity::getRecharge);
        entityOrderFields.add(CardRechargeEntity::getAfterRecharge);
        entityOrderFields.add(CardRechargeEntity::getRechargeType);
        entityOrderFields.add(CardRechargeEntity::getRechargeTime);
        entityOrderFields.add(CardRechargeEntity::getRechargeNum);
        entityOrderFields.add(CardRechargeEntity::getPayer);
        entityOrderFields.add(CardRechargeEntity::getTollCollector);
    }

    /**
     * 获取全部的 <strong>充值记录</strong> 列表
     */
    @GetMapping("all")
    public Object listAll(@PageableDefault(sort = {"gmtCreate"}) Pageable pageable, CardRechargeEntity entity) {
        LambdaQueryChainWrapper<CardRechargeEntity> lambdaQuery = buildLambdaQuery(entity);
        MyBatisUtils.lambdaQueryAddOrder(lambdaQuery, pageable, entityOrderFields);
        return success(lambdaQuery.list());
    }

    /**
     * 分页获取 <strong>充值记录</strong> 列表
     *
     * @param pageable 分页参数信息
     */
    @GetMapping
    public Object list(@PageableDefault(sort = {"rechargeTime"}) Pageable pageable, CardRechargeEntity entity) {
        LambdaQueryChainWrapper<CardRechargeEntity> lambdaQuery = buildLambdaQuery(entity);
        MyBatisUtils.lambdaQueryAddOrder(lambdaQuery, pageable, entityOrderFields);
        Page<CardRechargeEntity> page = lambdaQuery.page(MyBatisUtils.toPage(pageable, false));
        for (CardRechargeEntity record : page.getRecords()) {
            try {
                EmployeeEntity employeeEntity = employeeService.getById(record.getPerson());
                record.setPerson(employeeEntity.getName());
            } catch (Exception ignore) {
            }
        }
        return success(page);
    }

    /**
     * 获取一个 <strong>充值记录</strong>
     *
     * @param id 主键ID
     */
    @GetMapping("{id}")
    public Object info(@PathVariable String id) {
        return success(cardRechargeService.getById(id));
    }

    /**
     * 添加一个 <strong>充值记录</strong>
     *
     * @param entity 修改后的信息
     */
    @PostMapping
    public Object add(@RequestBody CardRechargeEntity entity) {
        cardRechargeService.saveCardRecharge(entity);

        HttpHeaders headers = new HttpHeaders();
        headers.add(HttpHeaders.LOCATION, String.valueOf(entity.getId()));
        return new ResponseEntity<>(headers, HttpStatus.CREATED);
    }

    /**
     * 修改一个 <strong>充值记录</strong>
     *
     * @param id     主键ID
     * @param entity 修改后的信息
     */
    @PutMapping("{id}")
    public Object update(@PathVariable String id, @RequestBody CardRechargeEntity entity) {
        entity.setId(id);
        cardRechargeService.updateCardRecharge(entity);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    /**
     * 删除一个 <strong>充值记录</strong>
     *
     * @param id 主键ID
     */
    @DeleteMapping("{id}")
    public Object delete(@PathVariable String id) {
        cardRechargeService.deleteCardRecharge(id);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * 删除多个 <strong>充值记录</strong>
     *
     * @param ids 主键ID列表
     */
    @DeleteMapping
    public Object deleteIds(@RequestBody List<String> ids) {
        cardRechargeService.deleteCardRecharge(ids);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * 构建查询条件内容
     *
     * @param entity 实体对象
     * @return lambda query chain wrapper
     */
    private LambdaQueryChainWrapper<CardRechargeEntity> buildLambdaQuery(CardRechargeEntity entity) {
        LambdaQueryChainWrapper<CardRechargeEntity> lambdaQuery = cardRechargeService.lambdaQuery();
        lambdaQuery.eq(StringUtils.isNotBlank(entity.getId()), CardRechargeEntity::getId, entity.getId());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getCardMember()), CardRechargeEntity::getCardMember, entity.getCardMember());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getPerson()), CardRechargeEntity::getPerson, entity.getPerson());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getPersonType()), CardRechargeEntity::getPersonType, entity.getPersonType());
        lambdaQuery.like(entity.getBalance() != null, CardRechargeEntity::getBalance, entity.getBalance());
        lambdaQuery.like(entity.getRecharge() != null, CardRechargeEntity::getRecharge, entity.getRecharge());
        lambdaQuery.like(entity.getAfterRecharge() != null, CardRechargeEntity::getAfterRecharge, entity.getAfterRecharge());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getRechargeType()), CardRechargeEntity::getRechargeType, entity.getRechargeType());
        lambdaQuery.like(entity.getRechargeTime() != null, CardRechargeEntity::getRechargeTime, entity.getRechargeTime());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getRechargeNum()), CardRechargeEntity::getRechargeNum, entity.getRechargeNum());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getPayer()), CardRechargeEntity::getPayer, entity.getPayer());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getTollCollector()), CardRechargeEntity::getTollCollector, entity
                .getTollCollector());
        return lambdaQuery;
    }

}
